<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JS回调函数的实现</title>
<script type="text/javascript" src="callback.js"></script>
<script type="text/javascript">
	function test1() {
		var p1 = document.getElementById("p1").value;
		var p2 = document.getElementById("p2").value;
		var obj = new CallBack();
		obj.test = function(p1, p2) {
			try {
				if (isNaN(p1)) {
					throw new Error('p1=' + p1 + ' 不是数字');
				}
				if (isNaN(p2)) {
					throw new Error('p2=' + p2 + ' 不是数字');
				}
				n1 = parseInt(p1);
				n2 = parseInt(p2);
				if (n1 > n2) {
					this.data = p1 + '大于' + p2;
				} else if (n1 < n2) {
					this.data = p1 + '小于' + p2;
				} else {
					this.data = p1 + '等于' + p2;
				}
				this.status = 'success';
			} catch (e) {
				this.status = 'error';
				this.msg = e.message;
			}
			setTimeout(this._back, 100);//给100毫秒空间执行success和error的初始化
			return this;
		}
		obj.test(p1, p2).success(function(data) {
			alert('suceess->' + data);
		}).error(function(data) {
			alert('error->' + data);
		});
	}
	function test2() {
		var url = document.getElementById("url").value;
		var username = document.getElementById("username").value;
		var passwod = document.getElementById("passwod").value;
		var http = new MyHttp();
		http.get(url, {
			"username" : username,
			"passwod" : passwod
		}).success(function(data) {
			alert(data);
		}).error(function(msg) {
			alert(msg);
		});
	}
	function test3() {
		var p1 = document.getElementById("mm1").value;
		var p2 = document.getElementById("mm2").value;
		var me = new CallBack();
		me.kill(p1, p2).success(function(data) {
			alert(data);
		}).error(function(msg) {
			alert(msg);
		});
	}
	function test4() {
		var url = document.getElementById("url4").value;
		var request = null;
		if (window.XMLHttpRequest) {
			request = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
				}
			}
		}
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				if (request.status == 200) {
					console.log('[code]:' + request.status);
					console.log('[status]:' + request.statusText);
					console.log('[type]:' + request.responseType);
					console.log('[text]:' + request.responseText);
					console.log('[body]:' + request.responseBody);
					console.log('[xml]:' + request.responseXML);
					alert(request.responseText);
				} else {
					alert('失败了');
				}
			}
		}
		request.open("POST", url, true);
		request.setRequestHeader("Content-Type","application/json");
		var user = "{\"username\":\"liuzy\",\"password\":\"yjwkhhbj\"}";
		request.send(user);
	}
</script>
</head>
<body>
	<center><h1>JS回调函数的实现</h1></center>
	<center><p>什么是回调？回调就是，把一个函数丢给另一个函数，拿到这个函数想什么时候跑就什么时候跑！</p></center>
	<fieldset>
		<legend>测试1</legend>
		数字一：<input type="text" id="p1" value="2" size="50"> <br /> 
		数字二：<input type="text" id="p2" value="4" size="50"> <br /> 
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" onClick="test1()" value="测试">
	</fieldset>
	<br />
	<fieldset>
		<legend>测试2</legend>
		地 址 ：<input type="text" id="url" value="http://www.liuzhongyang.com/AngularJsWeb/user/login" size="50"><br /> 
		用户名：<input type="text" id="username" value="admin" size="50"> <br /> 
		密 码 ：<input type="text" id="passwod" value="111111" size="50"> <br /> 
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" onClick="test2()" value="测试">
	</fieldset>
	<br />
	<fieldset>
		<legend>测试3</legend>
		杀掉她：<input type="text" id="mm1" value="lucy" size="50"> <br /> 
		杀掉她：<input type="text" id="mm2" value="amy" size="50"> <br /> 
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" onClick="test3()" value="测试">
	</fieldset>
	<br />
	<fieldset>
		<legend>测试4-证明了无法跨域</legend>
		XHR：<input type="text" id="url4" value="http://www.liuzhongyang.com/AngularJsWeb/user/login" size="50"> 
		<input type="button" onClick="test4()" value="测试">
		<br/><br/>
		<a href="index.html">上一页</a> <a href="jsonp.html">下一页</a>
		<br/><br/>
	</fieldset>
	<br />
</body>
</html>
<style type="text/css">
fieldset {width:780px;margin: 0px auto;}
</style>
